package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @param
 * @return
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    NursingProjectMapper nursingProjectMapper;

    //分页查询所有数据
    @Override
    public PageResponse selectByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> pageProjectVo = nursingProjectMapper.selectByPage(name, status);
        PageResponse<NursingProjectVo> of = PageResponse.of(pageProjectVo, NursingProjectVo.class);
        of.getRecords().forEach(vo -> {
            //查询每个护理项目被引用了多少次 将所有护理计划中的护理项目数量查出来
            Long count = nursingProjectMapper.getProgectRefCount(vo.getId());
            vo.setCount(count);
        });
        return of;
    }

    //新增护理项目
    @Override
    public void insertNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.insertNursingProject(nursingProject);
    }

    //查询回显
    @Override
    public NursingProjectVo selectNursingProjectById(Long id) {
        NursingProjectVo nursingProjectVo = nursingProjectMapper.selectNursingProjectById(id);
        return nursingProjectVo;
    }

    //修改护理项目
    @Override
    public void updateNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    //删除护理项目
    @Override
    public void deleteNursingProject(Long id) {
        nursingProjectMapper.deleteNursingProject(id);
    }

    //是否禁用
    @Override
    public void isStatusNursingProject(Long id, Integer status) {
        nursingProjectMapper.isStatusNursingProject(id, status);
    }

    //查询所有的护理项目,在护理计划中需要查出来
    @Override
    public List<NursingProjectVo> getAllNursingProject() {
        List<NursingProjectVo> list = nursingProjectMapper.getAllNursingProject();
        list.forEach(vo -> {
            //查询每个护理项目被引用了多少次 将所有护理计划中的护理项目数量查出来
            Long count = nursingProjectMapper.getProgectRefCount(vo.getId());
            vo.setCount(count);
        });
        return list;
    }
}
